package com.sh.sales.happysales.app.utils;


import android.util.Base64;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import java.security.Key;
import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;


/**
 * Created by zhouyucheng on 2017/9/14.
 */

public class IDEACode {
    /**
     * 密钥算法
     */
    public static final String KEY_ALGORITHM = "IDEA";
    /**
     * 加密/解密算法/工作模式/填充方式
     */
    // public static final String CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";

    public static final String CIPHER_ALGORITHM = "IDEA/ECB/ZeroBytePadding";

    /**
     * 生成密钥，只有bouncycastle支持
     *
     * @return byte[] 二进制密钥
     */
    public static byte[] initkey() throws Exception {
        //加入bouncyCastle支持
        Security.addProvider(new BouncyCastleProvider());
        //实例化密钥生成器
        KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM);
        //初始化密钥生成器，IDEA要求密钥长度为128位
        kg.init(128);
        //生成密钥
        SecretKey secretKey = kg.generateKey();
        //获取二进制密钥编码形式
        return secretKey.getEncoded();
    }

    /**
     * 转换密钥
     *
     * @param key 二进制密钥
     * @return Key 密钥
     */
    public static Key toKey(byte[] key) throws Exception {
        //实例化DES密钥
        //生成密钥
        SecretKey secretKey = new SecretKeySpec(key, KEY_ALGORITHM);
        return secretKey;
    }

    /**
     * 加密数据
     *
     * @param data 待加密数据
     * @param key  密钥
     * @return byte[] 加密后的数据
     */
    public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
        //加入bouncyCastle支持
        Security.addProvider(new BouncyCastleProvider());
        //还原密钥
        Key k = toKey(key);
        //实例化
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        //初始化，设置为加密模式
        cipher.init(Cipher.ENCRYPT_MODE, k);
        //执行操作
        return cipher.doFinal(data);
    }

    /**
     * 解密数据
     *
     * @param data 待解密数据
     * @param key  密钥
     * @return byte[] 解密后的数据
     */
    public static byte[] decrypt(byte[] data, byte[] key) throws Exception {
        //加入bouncyCastle支持
        Security.addProvider(new BouncyCastleProvider());
        //还原密钥
        Key k = toKey(key);
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        //初始化，设置为解密模式
        cipher.init(Cipher.DECRYPT_MODE, k);
        //执行操作
        return cipher.doFinal(data);
    }

    /**
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
//        String str = "0c0b0865209cfc8a2d191ddba1b6a432|100|0c0b0865209cfc8a2d191ddba1b6a432|123456789";
//        System.out.println("原文：" + str);
//        //初始化密钥
//        byte[] key = IDEACode.initkey();
//        System.out.println("密钥：" + Base64.encodeBase64String(key));
//        //加密数据
//        byte[] data = IDEACode.encrypt(str.getBytes(), "12345678".getBytes());
//        System.out.println("加密后：" + Base64.encodeBase64String(data));
//        //解密数据
//        data = IDEACode.decrypt(data, key);
//        System.out.println("解密后：" + new String(data));
        String a = "0c0b0865209cfc8a2d191ddba1b6a432";
        int b = 100;
        String c = "0c0b0865209cfc8a2d191ddba1b6a432";
        String d = "123456789";
        String e = "sky@w0rth-HighTO";
        String sign = getSignedData(a, b, c, d, e);
        System.out.println(sign);


    }

    private static String freshData(String accessToken, int userId, String reflashToken, String deviceId) {
        return accessToken + "|" + userId + "|" + reflashToken + "|" + deviceId;

    }

    public static String getSignedData(String accessToken, int userId, String reflashToken, String deviceId, String key) {
        String s = null;
        try {
            BouncyCastleProvider bouncyCastleProvider = new BouncyCastleProvider();
            if (null != Security.getProvider(bouncyCastleProvider.getName())) {
                Security.removeProvider(bouncyCastleProvider.getName());
            }
            String freshstr = freshData(accessToken, userId, reflashToken, deviceId);
            System.out.println("freshstr:" + freshstr);
            System.out.println("freshstrbyte:" + freshstr.getBytes());
            System.out.println("keybyte:" + key.getBytes());
            s = Base64.encodeToString(IDEACode.encrypt(freshstr.getBytes(), key.getBytes()), Base64.NO_WRAP);
            // s=LocalBase64.encode(IDEACode.encrypt(freshstr.getBytes(), key.getBytes()));
            System.out.println(s);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return s;
    }

}
